package com.lkm.app_ui.view.choiceCard;

import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.GridView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.lkm.comlib.help.ViewHelp;




public class ChoiceCard extends LinearLayout implements OnItemClickListener {
	
	public interface ChoiceCardBC{
		public void onChoiceChangeListener(ChoiceCard cc, IChoiceItem c, int index);
	}
	
	private ChoiceCardBC mChoiceCardBC;
	public void setChoiceCardBC(ChoiceCardBC l){
		mChoiceCardBC=l;
	}
	
	private GridView mGridView;
	private IChoiceItem [] choiceItems;
	private MAdapter mAdapter;
	private int itemViewHeigth = -1;
	
	public ChoiceCard(Context context) {
		super(context);

	}

	public ChoiceCard(Context context, AttributeSet attrs) {
		super(context, attrs);

	}
	private int layoutChoiceItemId;
	private int layoutUnChoiceItemId;

	private boolean isMultiple;//估计是多选意思


	public void init(boolean isMultiple,int layoutViewId,int layoutChoiceItemId,int layoutUnChoiceItemId,int numColumns){
		this.isMultiple=isMultiple;

		this.layoutChoiceItemId=layoutChoiceItemId;
		this.layoutUnChoiceItemId=layoutUnChoiceItemId;

		LayoutInflater.from(getContext()).inflate(layoutViewId, this);
		mGridView=(GridView) findViewById(android.R.id.list);
		mGridView.setNumColumns(numColumns);

		mGridView.setAdapter(mAdapter=new MAdapter());
		mGridView.setOnItemClickListener(this);
	}

	public void setItemViewHeigth(int itemViewHeigth) {
		this.itemViewHeigth = itemViewHeigth;
	}

	public GridView getGridView() {
		return mGridView;
	}



	public void binData( IChoiceItem [] choiceItems){


		if(!isMultiple) {
			boolean isOncehoice = false;
			for (IChoiceItem c : choiceItems) {

				if(isOncehoice){
					c.setIsChoice(false);
				}else{
					if(c.getIsChoice())
						isOncehoice=true;
				}


			}
		}

		this.choiceItems=choiceItems;



//				mGridView.setNumColumns(choiceItems.length);

		mAdapter.notifyDataSetChanged();
	}



	public void setNumColumns(int c){
		mGridView.setNumColumns(c);
	}
	
	public IChoiceItem [] getData(){
		return this.choiceItems;
	}

	

	private class MAdapter extends BaseAdapter{

		@Override
		public int getCount() {
			return choiceItems==null?0:choiceItems.length;
		}

		@Override
		public int getViewTypeCount() {
			return 2;
		}

		@Override
		public int getItemViewType(int position) {
			return choiceItems[position].getIsChoice()?0:1;
		}

		@Override
		public Object getItem(int arg0) {
			return null;
		}

		@Override
		public long getItemId(int arg0) {
			return 0;
		}

		@Override
		public View getView(int pos, View v, ViewGroup vg) {

			if(v==null) {

				v = LayoutInflater.from(getContext()).inflate(getItemViewType(pos)==0?layoutChoiceItemId:layoutUnChoiceItemId, vg, false);
				if (itemViewHeigth > 0)
					ViewHelp.setViewHeigth(v, itemViewHeigth);
			}

			TextView tv=(TextView) v.findViewById(android.R.id.title);

			IChoiceItem ci=choiceItems[pos];
			tv.setText(ci.getName());

			TextView tv2=(TextView) v.findViewById(android.R.id.text2);
			if(tv2!=null){
				tv2.setText(ci.getValue());
			}

			v.setTag(ci);
			return v;
		}
		
	}
	@Override
	public void onItemClick(AdapterView<?> arg0, View v, int pos, long arg3) {
		IChoiceItem ci=(IChoiceItem) v.getTag();
		onItemClick(ci,pos);
	}
	public void onItemClick(IChoiceItem ci,int index) {

		if(isMultiple){
			ci.setIsChoice( !ci.getIsChoice());
		}else{
			for (IChoiceItem c:choiceItems){
				c.setIsChoice(false);
			}
			ci.setIsChoice(true);
		}

		if(mChoiceCardBC!=null)mChoiceCardBC.onChoiceChangeListener(this, ci, index);
		mAdapter.notifyDataSetChanged();

	}

	public void onItemClick(Object id) {
		if (id == null) return;
		int index = -1;
		IChoiceItem choiceItem = null;
		for (IChoiceItem c : choiceItems) {
			index++;
			if (id.equals(c.getID())) {
				choiceItem = c;
				break;
			}
		}
		if (choiceItem != null)
			onItemClick(choiceItem, index);
	}
	
	
	
}
